package com.aaa.controller.tables;

import com.aaa.dao.FOrderMapper;
import com.aaa.dao.FOrderPMapper;
import com.aaa.entity.FOrder;
import com.aaa.entity.FOrderp;
import com.aaa.service.FOrderService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("forder")
public class FOrderController {
    @Resource
    FOrderService fOrderService;
    @Resource
    FOrderMapper fOrderMapper;
    @Resource
    FOrderPMapper fOrderPMapper;
    @RequestMapping("select")
    public List<FOrder> select(Integer pageCode,Integer pageSize,FOrder fOrder){
        System.out.println(pageCode+","+pageSize);
        return fOrderService.select(pageCode, pageSize,fOrder);
    }
    @RequestMapping("insert")
    public String insert(FOrder fOrder, FOrderp fOrderp){
        Integer a= fOrderMapper.insert(fOrder);
        QueryWrapper<FOrder> fOrderQueryWrapper = new QueryWrapper<>();
        fOrderQueryWrapper.select("id").eq("parcelid",fOrder.getParcelid());
        FOrder fOrder1 = fOrderMapper.selectList(fOrderQueryWrapper).get(0);
        long id= fOrder1.getId();
        System.out.println(id);
        fOrderp.setFOrder(id);
        Integer b= fOrderPMapper.insert(fOrderp);
        if(a>0&&b>0){
            return "提交成功";
        }else{
            return "提交失败";
        }
    }
    @RequestMapping("insert1")
    public Integer insert1(FOrder fOrder) {
        System.out.println(fOrder.getFUser()+"用户");
        return fOrderMapper.insert(fOrder);
    }
    @RequestMapping("delete")
    public String delete(Integer id){
        QueryWrapper<FOrderp> fOrderQueryWrapper = new QueryWrapper<>();
        fOrderQueryWrapper.eq("f_order",id);
        Integer b=fOrderPMapper.delete(fOrderQueryWrapper);
        Integer a= fOrderMapper.deleteById(id);
        if(a>0&&b>0){
            return "删除成功";
        }else{
            return "删除失败";
        }
    }
    @RequestMapping("update")
    public Boolean insert(FOrder fOrder){
        return new LambdaUpdateChainWrapper<FOrder>(fOrderMapper)
                .eq(FOrder::getId,fOrder.getId())
                .set(FOrder::getState, fOrder.getState())
                .update()
                ;
    }

    @RequestMapping("insert2")
    public String insert2(FOrder fOrder,Integer book[],Integer num[],Integer price1[]){
        //System.out.println(book+","+num+","+price1);
        //http://localhost:8080/forder/insert?id=0&fUser=13&parcelid=21002&book=1,2,3&price1=99,33,33&num=1,1,1&fSite=25&state=4
        Integer sumPrice=0;
        for (int i=0;i<price1.length;i++){
            System.out.println(price1[i]+","+num[i]+","+book[i]);
            Integer a=price1[i]*num[i];
            sumPrice+=a;
        }
        System.out.println(sumPrice+"这是总金额");
        fOrder.setPrice(sumPrice);
        Integer a= fOrderMapper.insert(fOrder);
        QueryWrapper<FOrder> fOrderQueryWrapper = new QueryWrapper<>();
        fOrderQueryWrapper.select("id").eq("parcelid",fOrder.getParcelid());
        FOrder fOrder1 = fOrderMapper.selectList(fOrderQueryWrapper).get(0);
        long id= fOrder1.getId();
        System.out.println(id);
        FOrderp fOrderp=new FOrderp();
        fOrderp.setFOrder(id);
        //Integer b= fOrderPMapper.insert(fOrderp);
        for (int i=0;i<book.length;i++){
           fOrderp.setBook(book[i]);
           fOrderp.setNum(num[i]);
           fOrderp.setPrice(price1[i]);
           Integer moneys=num[i]*price1[i];
           fOrderp.setMoney(moneys);
           fOrderPMapper.insert(fOrderp);
        }
        if(a>0){
            return "提交成功";
        }else{
            return "提交失败";
        }
    }
}
